package com.xzp.blog.controller;


import com.xzp.blog.consts.Message;
import com.xzp.blog.controller.base.BaseController;
import com.xzp.blog.entity.About;
import com.xzp.blog.entity.Article;
import com.xzp.blog.entity.dto.form.AdminUserPwdModifyForm;
import com.xzp.blog.entity.dto.form.BlogAddForm;
import com.xzp.blog.entity.dto.form.BlogModifyForm;
import com.xzp.blog.entity.dto.request.TableKeyModel;
import com.xzp.blog.entity.vo.AboutModifyView;
import com.xzp.blog.entity.vo.BlogModifyView;
import com.xzp.blog.service.IAboutService;
import com.xzp.blog.service.IAdminService;
import com.xzp.blog.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.xml.ws.http.HTTPBinding;

import java.util.List;

import static com.xzp.blog.consts.ViewConsts.VIEW_ARTICLE;

/**
 * 后台控制器
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController{
    @Autowired
    private IArticleService articleService;  //博客文章管理服务

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IAboutService aboutService;

    /**
     * 博客管理首页
     */
    @RequestMapping({"","/index","/blogmanage"})
    public String adminIndex(HttpServletRequest request, Model model){
        return "admin/blog_manage";
    }

    /**
     * 后台登录
     */
    @GetMapping("/login")
    public String adminLogin(HttpServletRequest request,Model model){
        return "admin/userlogin";
    }

    /**
     * 写博客页面-新建博客文章
     */
    @GetMapping("/blogadd")
    public String adminBlogAdd(HttpServletRequest request,Model model){
        return "admin/blogadd";
    }

    /**
     * 修改博客
     */
    @GetMapping("/blogmodify/{id}")
    public String adminBlogModify(HttpServletRequest request, Model model, @PathVariable Integer id) throws Exception {
        Article article = articleService.getArticleById(id); //获取文章
        //将文章转化为博客编辑视图类
        BlogModifyView modifyView = new BlogModifyView(article);
        addModelAtt(model,VIEW_ARTICLE,modifyView);
        return "admin/blog_modify";
    }

    /**
     * 发布文章
     */
    @PostMapping("/blogadd.f")
    public String adminBlogAdd(BlogAddForm blogAddForm){

        articleService.blogAdd(blogAddForm);   //添加博客文章
        return "redirect:/admin/index";  //重定向到后台主页

    }

    /**
     * 修改文章
     */
    @PostMapping("blog_modify.f")
    public String adminBlodModify(BlogModifyForm blogModifyForm){
       articleService.blogModify(blogModifyForm);
       return "redirect:/admin/index";
    }


    /**
     * 后台加载博客列表
     * @return 文章表格视图类 列表
     */
    @GetMapping("/blog_list.j")
    @ResponseBody        //直接以对象返回
    public Object adminBlogList(){
        return articleService.getArticleList();
    }

    /**
     * 批量删除博客
     */
    @DeleteMapping("/blog_delete.j")
    @ResponseBody
    public Object adminBlogDelete(@RequestBody TableKeyModel model){
        articleService.blogDelete(model);
        return responseSimpleOK();
    }

    /**
     * 后台用户管理页面
     */
    @GetMapping("/admin_user_manage")
    public String pAdminUserManage() {
        return "admin/admin_user_manage";
    }

    /**
     * 后台用户管理 json 获取所有用户
     */
    @GetMapping("/admin_user.j")
    @ResponseBody
    public Object jAdminUserList() {
        return adminService.getAdminUserJson();
    }

    /**
     * 后台用户批量删除
     */
    @DeleteMapping("/admin_use_delete.j")
    @ResponseBody
    public Object jAdminUserDelete(@RequestBody TableKeyModel model) {
        adminService.deleteAdminUser(model);
        return responseSimpleOK();
    }

    /**
     * 后台用户密码修改页面
     */
    @GetMapping("/admin_user_pwd_modify")
    public String adminUserPwdModify() {
        return "admin/admin_user_pwd_modify";
    }

    /**
     * 后台用户密码修改
     */
    @PostMapping("/admin_user_pwd_modify.f")
    @ResponseBody
    public Object fAdminUserPwdModify(@Valid AdminUserPwdModifyForm form, BindingResult bindingResult, HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            return responseSimpleError();
        }
        String result = adminService.modifyUserPwd(form, request);

        if (result.equals(Message.MODIFY_SUCCESS)){
            return responseSimpleOK();
        }else {
           return responseSimpleError();
        }

    }

    @GetMapping("/about_modify")
    public String updateAbout(Model model) throws Exception {
        About article = aboutService.getAbout();
        AboutModifyView view = new AboutModifyView(article);
        addModelAtt(model, VIEW_ARTICLE, view);
        return "admin/about_modify";
    }

}
